<?php //echo "<pre>"; print_r($pBerkas);die();?>
<?php $this -> load -> view('header'); ?>
<!-- BEGIN CONTAINER -->
<div class="page-container row-fluid">
	<?php //$this->load->view('sidebar');?>
	<!-- BEGIN PAGE -->
	<div class="page-content">
		<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
		<div id="portlet-config" class="modal hide">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button"></button>
				<h3>portlet Settings</h3>
			</div>
			<div class="modal-body">
				<p>
					Here will be a configuration form
				</p>
			</div>
		</div>
		<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
		<!-- BEGIN PAGE CONTAINER-->
		<div class="container-fluid">
			<!-- BEGIN PAGE HEADER-->
			<div class="row-fluid">
				<div class="span12">
					
					<!-- BEGIN PAGE TITLE & BREADCRUMB-->
					<h3 class="page-title">Daftar Laporan</h3>
					<?php $this -> load -> view('breadcrumb'); ?>
					<!-- END PAGE TITLE & BREADCRUMB-->
				</div>
			</div>
			<!-- END PAGE HEADER-->
			<!-- BEGIN PAGE CONTENT-->
			<div class="row-fluid">
				<div class="span12">
					<!-- BEGIN EXAMPLE TABLE PORTLET-->
					<div class="portlet box blue">
						<div class="portlet-title">
							<h4><i class="icon-edit"></i></h4>
							<!--<div class="tools" >
								<a href="javascript:;" class="reload"></a>
							</div>-->
														
							<div class="actions" style="padding-right: 0px">
								<!--<a href="<?php echo base_url()?>dashboard/createEditReport" class="btn green"><i class="icon-plus"></i> Add</a>-->								
								<!--<a href="#" class="btn green"><i class="icon-print"></i> Print</a>
								<a href="#" class="btn green"><i class="icon-tasks"></i> Pdf</a>
								<a href="#" class="btn green"><i class="icon-envelope"></i> Excel</a>-->								
							</div>
							
						</div>
						<div class="portlet-body">
							<?php $this->load->view('searchBar', $searchBar);?>
							<table class="table table-striped table-hover table-bordered" >
								<thead>
									<tr>
										<th>No</th>
										<th>Kode Laporan</th>
										<th>Nama Perpustakaan</th>
										<th>Jenis Berkas</th>
										<!--<th>No Indikator</th>-->
										<th>Tanggal Pelaporan</th>
										<th>Pelapor</th>
										<th>Status</th> 
										<th>Tindakan</th>
									</tr>
								</thead>
								<tbody>
								<?php
									$i=1;
									$isParalelFlowBerkasTraining = FALSE;
									$tipeBerkas = ""; 
									$isUserVerHasSubmit = FALSE;
									foreach ($pBerkas['data'] as $berkas){ 
										if ($berkas->getTipe()->getId() == "TRAINING"){
											$criteria = array("id" => $berkas);	
											$berkasTraining = $this -> em -> getRepository('\models\entities\BerkasTraining')->findOneBy($criteria);
											$tipeBerkas = $berkas->getTipe()->getNama(). " - " . $berkasTraining->getJenisTraining()->getNama();
										/*	
											if ($berkasTraining->getJenisTraining()->getJenisFlow() == 2){
												$isParalelFlowBerkasTraining = TRUE;
												$criteria = array("id" => $berkas);	
												$inbox = $this -> em -> getRepository('\models\entities\Inbox')->findOneBy($criteria);
												if ($inbox->getLastVer() == $user->getUserId()){
													$isUserVerHasSubmit = TRUE;
												}			
											} */
										} else {
											$tipeBerkas = $berkas->getTipe()->getNama();	 
										} 
										$criteria = array("id" => $berkas);	
										$inbox = $this -> em -> getRepository('\models\entities\Inbox')->findOneBy($criteria);
										$enabledSubmitBtn = TRUE;
										if ($inbox->getProcedureStep()->getIsParalel() == 1){
											if ($inbox->getProcedureStep()->getVerifyByJumlah() != NULL && $inbox->getProcedureStep()->getVerifyByJumlah()>0){
												if ($inbox->getLastVer() == $user->getUserId()){
													$enabledSubmitBtn = FALSE;
												}
											}	
											
											if ($inbox->getProcedureStep()->getVerifyByJabatan() != NULL && $inbox->getProcedureStep()->getVerifyByJabatan() != $user->getJabatan()->getId()){
												$enabledSubmitBtn = FALSE;
											}
										}
										
										?>
									<tr class="" >
										<td><?php echo $i;?></td>
										<td><?php echo $berkas->getId();?></td>
										<td><?php echo $berkas->getPerpustakaan()->getNama();?></td>
										<td><?php echo $tipeBerkas;?></td>
										
										<td class="center"><?php echo ($berkas->getEventDate() != NULL) ? $berkas->getEventDate()->format('d-m-Y') : "";?></td>
										<td class="center"><?php echo ($berkas->getReporter() != NULL) ? $berkas->getReporter() : "";?></td>
										<!--<td></td>-->
										<td>
											<?php if ($berkas->getInbox()->getCondition() == "NOK"){
														echo '<span class="label label-important">'.$berkas->getInbox()->getCondition().'</span>';
												  } 													
												  else {
												  		echo '<span class="label label-success">'.$berkas->getInbox()->getCondition().'</span>';
												  }
												  		
											?></td>
										<td>
											<?php
												if ($inbox->getProcedureStep()->getIsParalel() == 1 && $enabledSubmitBtn === FALSE){
													// cari berkas_id, user, profile, procedure_step, action
													$cond = "REVIEW";
													$berkasId = $berkas->getId();
													$userId = $user->getUserId();
													//$procedureStepId = $inbox->getProcedureStep()->getId();
													$lastInboxActivity = $this -> em -> getRepository('\models\entities\BerkasActivity')->findUserActivityInbox($berkasId, $userId, $cond);
													$udahReview = ""; 
													if (count($lastInboxActivity) > 0){
														$udahReview = "Sudah Review";
													}
													
													if ($udahReview==""){
														echo '<a class="btn blue mini" href="'.base_url().'inbox/createEdit/'.$selectedProfile.'/'.$berkas->getId().'">Review</a>&nbsp;';
													} else {
														echo '<a class="btn blue mini" href="'.base_url().'inbox/createEdit/'.$selectedProfile.'/'.$berkas->getId().'">view</a>&nbsp;'.$udahReview;
													}
												} else { ?>
											<a class="btn blue mini" href="<?php echo base_url().'inbox/createEdit/'.$selectedProfile.'/'.$berkas->getId()?>">View</a>&nbsp;	
										<!--	<a class="btn green mini" href="#" id="submitBtn" onclick="submitReport(event,'<?php echo $berkas->getId();?>','<?php echo $berkas->getInbox()->getCondition()?>')">Submit</a>&nbsp;
												<a class="btn red mini" id="returnBtn" onclick="returnReport(event,'<?php echo $berkas->getId();?>','<?php echo $berkas->getInbox()->getCondition()?>')">Return</a>&nbsp; -->
											<?php											
												}	
											?>
										</td>
									</tr>		
								<?php	$i++;} ?>
								</tbody>
							</table>
							<div class="row-fluid">
								<?php $this->load->view('paging', $pBerkas);?>
							</div>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>
			<!-- END PAGE CONTENT -->
		</div>
		<!-- END PAGE CONTAINER-->
	</div>
	<!-- END PAGE -->
</div>
<!-- END CONTAINER -->
<div style="display: none">
	<form action="" method="post" id="formReport">
		<input type="hidden" name="Berkas.id" id="inputId"/>
		<input type="hidden" name="BerkasComment.text" id="berkasNote"/>
		<input type="hidden" name="Profile.id" id="inputProfile" value="<?php echo $selectedProfile?>"/>
	</form>
</div>

<!-- DIALOG -->
<div id="myNote" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
		<h3 id="myModalLabel1">Add Comment</h3>		
	</div>
<div class="modal-body">
	<div class="portlet-body fuelux">
		<textarea id="note"></textarea>	
	</div>	
</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
		<button class="btn yellow" id="saveNote">Save</button>
	</div>
</div>
<!-- END DIALOG -->	
<?php $this->load->view('footer')?>
<script>
	$(document).ready(function() {
		viewBerkas = function(id){
			window.location = '<?php echo base_url().'inbox/createEdit/'.$selectedProfile.'/'?> + id;
		}
		
		submitReport = function(event, id, status){
			event.preventDefault();
			$('#inputId').val(id);
			$('#formReport').attr('action', "<?php echo base_url()?>inbox/submit");
			$('#myNote').modal('show');						  		
	  	};
	  	
	  	returnReport = function(event, id, status){
			event.preventDefault();
			$('#inputId').val(id);
			$('#formReport').attr('action', "<?php echo base_url()?>dashboard/returnReport");
			$('#myNote').modal('show');			  		
	  	};
	  	
	  	$('#saveNote').click(function(){
			var textNote = $('#note').val();
			$('#berkasNote').val(textNote);			
			if (textNote==''){
				alert('Harus input comment');
				return;
			} else {
				$('#myNote').modal('hide');
				$('#formReport').submit();
			}
		});
	});
	
</script>
